/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.GUIUtils;

/**
 *
 * @author Jeppe Andersen
 */
public class HandleplanPanel extends javax.swing.JPanel {

    private Session session;
    private Laerer currentLaerer;
    private Elev currentElev;
    private Fag currentFag;
    private ArrayList handleplaner;
    private ArrayList evalueringer;

    /**
     * Creates new form HandleplanPanel
     */
    public HandleplanPanel(Session session) {
        handleplaner = new ArrayList();
        evalueringer = new ArrayList();
        this.session = session;
        this.currentLaerer = null;
        initComponents();
    }

    public HandleplanPanel(Session session, Laerer laerer) {
        handleplaner = new ArrayList();
        evalueringer = new ArrayList();
        this.session = session;
        this.currentLaerer = laerer;
        initComponents();
    }

    protected void update() {
        int index = cbElev.getSelectedIndex();
        int indexFag = cbFag.getSelectedIndex();
        if (currentLaerer == null) {
            int indexLaerer = cblaerer.getSelectedIndex();
            // Updater lærer combobox
            cblaerer.removeAllItems();
            cblaerer.addItem("Lærer");
            String hql = "from Laerer";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                List resultList = q.list();
                for (int i = 0; i < resultList.size(); i++) {
                    cblaerer.addItem((Laerer) resultList.get(i));
                }
                session.getTransaction().commit();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }
            //ryd alle felter
            GUIUtils.clearFields(this);
            // set nogle cb til hvad de var før update
            cblaerer.setSelectedIndex(indexLaerer);
        } else if (currentLaerer != null) {
            cblaerer.removeAllItems();
            cblaerer.addItem(currentLaerer);
            cblaerer.setEnabled(false);
            validate();
            GUIUtils.clearFields(this);
            getStudents();
        }

        cbElev.setSelectedIndex(index);
        cbFag.setSelectedIndex(indexFag);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGemNyEvaluering = new javax.swing.JButton();
        jScrollPane7 = new javax.swing.JScrollPane();
        txtaEvaluering = new javax.swing.JTextArea();
        btnSletEvalueringBeskrivelse = new javax.swing.JButton();
        btnGemEvalueringBeskrivelse = new javax.swing.JButton();
        btnInsertEvalueringBeskrivelse = new javax.swing.JButton();
        txtNyEvaluering = new javax.swing.JTextField();
        cbGemteEvalueringer = new javax.swing.JComboBox();
        jLabel29 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listEvalueringer = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        btnSletEvaluering = new javax.swing.JButton();
        btnSletHandlePlan = new javax.swing.JButton();
        btnGemNyHandleplan = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listHandleplaner = new javax.swing.JList();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtaHandleplan = new javax.swing.JTextArea();
        cbFag = new javax.swing.JComboBox();
        cbElev = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        cblaerer = new javax.swing.JComboBox();
        btnGemHandleplan = new javax.swing.JButton();
        btnGemEvaluering = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setLayout(null);

        btnGemNyEvaluering.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGemNyEvaluering.setText("Gem Ny Evaluering");
        btnGemNyEvaluering.setEnabled(false);
        btnGemNyEvaluering.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemNyEvalueringActionPerformed(evt);
            }
        });
        add(btnGemNyEvaluering);
        btnGemNyEvaluering.setBounds(370, 540, 140, 23);

        txtaEvaluering.setColumns(20);
        txtaEvaluering.setRows(5);
        jScrollPane7.setViewportView(txtaEvaluering);

        add(jScrollPane7);
        jScrollPane7.setBounds(210, 370, 300, 160);

        btnSletEvalueringBeskrivelse.setText("Slet favorit");
        btnSletEvalueringBeskrivelse.setEnabled(false);
        btnSletEvalueringBeskrivelse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletEvalueringBeskrivelseActionPerformed(evt);
            }
        });
        add(btnSletEvalueringBeskrivelse);
        btnSletEvalueringBeskrivelse.setBounds(760, 430, 90, 23);

        btnGemEvalueringBeskrivelse.setText("Gem ny favorit ");
        btnGemEvalueringBeskrivelse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemEvalueringBeskrivelseActionPerformed(evt);
            }
        });
        add(btnGemEvalueringBeskrivelse);
        btnGemEvalueringBeskrivelse.setBounds(620, 430, 110, 23);

        btnInsertEvalueringBeskrivelse.setText("<< Indsæt");
        btnInsertEvalueringBeskrivelse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertEvalueringBeskrivelseActionPerformed(evt);
            }
        });
        add(btnInsertEvalueringBeskrivelse);
        btnInsertEvalueringBeskrivelse.setBounds(520, 400, 90, 23);
        add(txtNyEvaluering);
        txtNyEvaluering.setBounds(620, 400, 230, 20);

        cbGemteEvalueringer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Favoritter", "eks1", "eks2" }));
        cbGemteEvalueringer.setEnabled(false);
        cbGemteEvalueringer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbGemteEvalueringerActionPerformed(evt);
            }
        });
        add(cbGemteEvalueringer);
        cbGemteEvalueringer.setBounds(520, 370, 330, 20);

        jLabel29.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel29.setText("Evaluering");
        add(jLabel29);
        jLabel29.setBounds(210, 350, 80, 14);

        listEvalueringer.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Evaluering_Dato", "Evaluering_Dato1" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listEvalueringer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listEvalueringerMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listEvalueringer);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 370, 170, 160);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Evalueringer");
        add(jLabel2);
        jLabel2.setBounds(10, 350, 90, 14);

        btnSletEvaluering.setText("Slet");
        btnSletEvaluering.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletEvalueringActionPerformed(evt);
            }
        });
        add(btnSletEvaluering);
        btnSletEvaluering.setBounds(10, 540, 50, 23);

        btnSletHandlePlan.setText("Slet");
        btnSletHandlePlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletHandlePlanActionPerformed(evt);
            }
        });
        add(btnSletHandlePlan);
        btnSletHandlePlan.setBounds(10, 290, 50, 23);

        btnGemNyHandleplan.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGemNyHandleplan.setText("Gem Ny Handleplan");
        btnGemNyHandleplan.setEnabled(false);
        btnGemNyHandleplan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemNyHandleplanActionPerformed(evt);
            }
        });
        add(btnGemNyHandleplan);
        btnGemNyHandleplan.setBounds(700, 290, 150, 23);

        listHandleplaner.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Handleplan_Dato", "Handleplan_Dato1" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listHandleplaner.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listHandleplanerMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listHandleplaner);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 120, 170, 160);

        txtaHandleplan.setColumns(20);
        txtaHandleplan.setRows(5);
        jScrollPane5.setViewportView(txtaHandleplan);

        add(jScrollPane5);
        jScrollPane5.setBounds(210, 60, 640, 220);

        cbFag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fag" }));
        cbFag.setEnabled(false);
        cbFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFagActionPerformed(evt);
            }
        });
        add(cbFag);
        cbFag.setBounds(10, 70, 140, 20);

        cbElev.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elev", "elev1", "elev2" }));
        cbElev.setEnabled(false);
        cbElev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbElevActionPerformed(evt);
            }
        });
        add(cbElev);
        cbElev.setBounds(10, 40, 140, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Handleplaner");
        add(jLabel1);
        jLabel1.setBounds(10, 100, 90, 14);

        jLabel28.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel28.setText("Handleplan");
        add(jLabel28);
        jLabel28.setBounds(210, 40, 80, 14);

        cblaerer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lærer" }));
        cblaerer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cblaererActionPerformed(evt);
            }
        });
        add(cblaerer);
        cblaerer.setBounds(10, 10, 140, 20);

        btnGemHandleplan.setText("Gem handleplan");
        btnGemHandleplan.setEnabled(false);
        btnGemHandleplan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemHandleplanActionPerformed(evt);
            }
        });
        add(btnGemHandleplan);
        btnGemHandleplan.setBounds(70, 290, 110, 23);

        btnGemEvaluering.setText("Gem evaluering");
        btnGemEvaluering.setEnabled(false);
        btnGemEvaluering.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemEvalueringActionPerformed(evt);
            }
        });
        add(btnGemEvaluering);
        btnGemEvaluering.setBounds(70, 540, 110, 23);
        add(jSeparator1);
        jSeparator1.setBounds(0, 330, 1170, 10);
    }// </editor-fold>//GEN-END:initComponents

    private void btnInsertEvalueringBeskrivelseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertEvalueringBeskrivelseActionPerformed
        if (txtNyEvaluering.isEnabled() && txtNyEvaluering.getText().length() > 0) {
            txtaEvaluering.append(txtNyEvaluering.getText());
        } else if (cbGemteEvalueringer.getSelectedIndex() > 0) {
            Laererevaluering lev = (Laererevaluering) cbGemteEvalueringer.getSelectedItem();
            txtaEvaluering.append(lev.getId().getEvaluering());
        }
    }//GEN-LAST:event_btnInsertEvalueringBeskrivelseActionPerformed

    private void cbGemteEvalueringerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbGemteEvalueringerActionPerformed
        if (cbGemteEvalueringer.getSelectedIndex() != 0) {
            txtNyEvaluering.setEnabled(false);
            btnSletEvalueringBeskrivelse.setEnabled(true);
        } else {
            txtNyEvaluering.setEnabled(true);
            btnSletEvalueringBeskrivelse.setEnabled(false);
        }
    }//GEN-LAST:event_cbGemteEvalueringerActionPerformed

    private void cbElevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbElevActionPerformed
        ArrayList fag = new ArrayList();
        if (cbElev.getSelectedIndex() > 0) {
            cbFag.setEnabled(true);
            currentElev = (Elev) cbElev.getSelectedItem();
            ArrayList holdfaglist = new ArrayList();
            ArrayList laererfaglist = new ArrayList();
            holdfaglist.addAll(currentElev.getHold().getHoldfags());
            laererfaglist.addAll(currentLaerer.getHoldfags());
            for (int i = 0; i < holdfaglist.size(); i++) {
                Holdfag elevfag = ((Holdfag) holdfaglist.get(i));
                for (int j = 0; j < laererfaglist.size(); j++) {
                    Holdfag laererfag = ((Holdfag) laererfaglist.get(j));
                    if (laererfag.equals(elevfag)) {
                        fag.add(elevfag.getFag());
                    }
                }
            }
            cbFag.removeAllItems();
            cbFag.addItem("Fag");
            for (int i = 0; i < fag.size(); i++) {
                cbFag.addItem(fag.get(i));
            }
        } else {
            cbFag.setSelectedIndex(0);
            cbFag.setEnabled(false);
        }
    }//GEN-LAST:event_cbElevActionPerformed

    private void getStudents() {
        if (cblaerer.getSelectedIndex() > 0 || currentLaerer != null) {
            // Hent lærerens elever
            cbElev.removeAllItems();
            cbElev.addItem("Elev");
            Laerer l;
            if (currentLaerer != null) {
                l = currentLaerer;
            } else {
                l = (Laerer) cblaerer.getSelectedItem();
                currentLaerer = (Laerer) cblaerer.getSelectedItem();
            }
            ArrayList holdfags = new ArrayList();
            ArrayList elever = new ArrayList();
            ArrayList finallist = new ArrayList();
            holdfags.addAll(l.getHoldfags());
            for (int i = 0; i < holdfags.size(); i++) {
                Holdfag hf = (Holdfag) holdfags.get(i);
                elever.addAll(hf.getHold().getElevs());
                for (int j = 0; j < elever.size(); j++) {
                    Elev e = (Elev) elever.get(j);
                    if (!finallist.contains(e)) {
                        finallist.add(e);
                        cbElev.addItem(e);
                    }
                }
            }
            cbElev.setEnabled(true);

            // Hent lærerens evalueringer
            cbGemteEvalueringer.setEnabled(true);
            ArrayList ge = new ArrayList();
            ge.addAll(l.getLaererevaluerings());
            cbGemteEvalueringer.removeAllItems();
            cbGemteEvalueringer.addItem("Favoritter");
            for (int i = 0; i < ge.size(); i++) {
                cbGemteEvalueringer.addItem(ge.get(i));
            }
        } else {
            cbElev.setSelectedIndex(0);
            cbFag.setSelectedIndex(0);
            cbGemteEvalueringer.setSelectedIndex(0);
            cbElev.setEnabled(false);
            cbFag.setEnabled(false);
            cbGemteEvalueringer.setEnabled(false);
        }
    }
    private void cblaererActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cblaererActionPerformed
        getStudents();
    }//GEN-LAST:event_cblaererActionPerformed

    private void listHandleplanerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listHandleplanerMouseClicked
        if (!handleplaner.isEmpty()) {
            btnGemHandleplan.setEnabled(true);
            txtaHandleplan.setText(((Handleplan) handleplaner.get(listHandleplaner.getSelectedIndex())).getBeskrivelse());
        } else {
            btnGemHandleplan.setEnabled(false);
        }
    }//GEN-LAST:event_listHandleplanerMouseClicked

    private void cbFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFagActionPerformed
        handleplaner = new ArrayList();
        evalueringer = new ArrayList();
        ArrayList hTemp = new ArrayList();
        ArrayList eTemp = new ArrayList();

        if (cbFag.getSelectedIndex() > 0) {
            currentFag = (Fag) cbFag.getSelectedItem();
            hTemp.addAll(currentElev.getHandleplans());
            eTemp.addAll(currentElev.getEvaluerings());

            for (int i = 0; i < hTemp.size(); i++) {
                if (((Handleplan) hTemp.get(i)).getFag().equals(currentFag)) {
                    handleplaner.add(hTemp.get(i));
                }
            }

            for (int i = 0; i < eTemp.size(); i++) {
                if (((Evaluering) eTemp.get(i)).getFag().equals(currentFag)) {
                    evalueringer.add(eTemp.get(i));
                }
            }
            
            listHandleplaner.setListData(handleplaner.toArray());
            listEvalueringer.setListData(evalueringer.toArray());
            txtaEvaluering.setText("");
            txtNyEvaluering.setText("");
            txtaHandleplan.setText("");
            btnGemNyEvaluering.setEnabled(true);
            btnGemNyHandleplan.setEnabled(true);
            btnInsertEvalueringBeskrivelse.setEnabled(true);
        } else {
            btnGemNyEvaluering.setEnabled(false);
            btnGemEvaluering.setEnabled(false);
            btnGemHandleplan.setEnabled(false);
            btnGemNyHandleplan.setEnabled(false);
            btnInsertEvalueringBeskrivelse.setEnabled(false);
            listHandleplaner.setListData(handleplaner.toArray());
            listEvalueringer.setListData(evalueringer.toArray());
            txtaEvaluering.setText("");
            txtNyEvaluering.setText("");
            txtaHandleplan.setText("");
        }

    }//GEN-LAST:event_cbFagActionPerformed

    private void btnSletHandlePlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletHandlePlanActionPerformed

        if (!listHandleplaner.isSelectionEmpty()) {
            Handleplan h = (Handleplan) handleplaner.get(listHandleplaner.getSelectedIndex());
            session.beginTransaction();
            currentElev.getHandleplans().remove(h);
            session.update(currentElev);
            handleplaner.remove(listHandleplaner.getSelectedIndex());
            listHandleplaner.setListData(handleplaner.toArray());
            txtaHandleplan.setText("");
            session.getTransaction().commit();
        }
        btnGemHandleplan.setEnabled(false);
    }//GEN-LAST:event_btnSletHandlePlanActionPerformed

    private void btnGemNyHandleplanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemNyHandleplanActionPerformed
        if (txtaHandleplan.getText().length() > 0) {
            session.beginTransaction();
            HandleplanId hpid = new HandleplanId(currentElev.getCpr(), currentFag.getId(), new Date());
            Handleplan hp = new Handleplan(hpid, currentElev, currentFag, txtaHandleplan.getText());
            try {
                currentElev.getHandleplans().add(hp);
                session.update(currentElev);
                session.getTransaction().commit();
                update();
            } catch (NonUniqueObjectException e) {
                currentElev.getHandleplans().remove(hp);
                JOptionPane.showMessageDialog(null, "Der findes allerede en anden handleplan på denne dato", "Info", 2);
            }


        } else {
            if (txtaHandleplan.getText().length() == 0) {
                txtaHandleplan.setText("Der mangler tekst!!!");
            }
        }
    }//GEN-LAST:event_btnGemNyHandleplanActionPerformed

    private void btnGemHandleplanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemHandleplanActionPerformed
        if (listHandleplaner.getSelectedValue() != null) {
            session.beginTransaction();
            Handleplan hp = (Handleplan) listHandleplaner.getSelectedValue();
            hp.setBeskrivelse(txtaHandleplan.getText());
            session.update(hp);
            session.getTransaction().commit();
            update();
        }
    }//GEN-LAST:event_btnGemHandleplanActionPerformed

    private void listEvalueringerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listEvalueringerMouseClicked
        if (!evalueringer.isEmpty()) {
            btnGemEvaluering.setEnabled(true);
            txtaEvaluering.setText(((Evaluering) evalueringer.get(listEvalueringer.getSelectedIndex())).getBeskrivelse());
        } else {
            btnGemEvaluering.setEnabled(false);
        }

    }//GEN-LAST:event_listEvalueringerMouseClicked

    private void btnSletEvalueringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletEvalueringActionPerformed
        if (!listEvalueringer.isSelectionEmpty()) {
            Evaluering e = (Evaluering) evalueringer.get(listEvalueringer.getSelectedIndex());
            session.beginTransaction();
            currentElev.getEvaluerings().remove(e);
            session.update(currentElev);
            evalueringer.remove(listEvalueringer.getSelectedIndex());
            listEvalueringer.setListData(evalueringer.toArray());
            txtaEvaluering.setText("");
            session.getTransaction().commit();
        }
    }//GEN-LAST:event_btnSletEvalueringActionPerformed

    private void btnGemNyEvalueringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemNyEvalueringActionPerformed
        if (txtaEvaluering.getText().length() > 0) {
            session.beginTransaction();
            EvalueringId evid = new EvalueringId(currentElev.getCpr(), currentFag.getId(), new Date());
            Evaluering ev = new Evaluering(evid, currentElev, currentFag, txtaEvaluering.getText());
            try {
                currentElev.getEvaluerings().add(ev);
                session.update(currentElev);
                session.getTransaction().commit();
                update();
            } catch (NonUniqueObjectException e) {
                currentElev.getEvaluerings().remove(ev);
                JOptionPane.showMessageDialog(null, "Der findes allerede en anden evaluering på denne dato", "Info", 2);
            }


        } else {
            if (txtaEvaluering.getText().length() == 0) {
                txtaEvaluering.setText("Der mangler tekst!!!");
            }
        }
    }//GEN-LAST:event_btnGemNyEvalueringActionPerformed

    private void btnGemEvalueringBeskrivelseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemEvalueringBeskrivelseActionPerformed
        if (txtNyEvaluering.getText().length() > 0) {
            session.beginTransaction();
            LaererevalueringId levid = new LaererevalueringId(currentLaerer.getId(), txtNyEvaluering.getText());
            Laererevaluering lev = new Laererevaluering(levid, currentLaerer);
            try {
                currentLaerer.getLaererevaluerings().add(lev);
                session.update(currentLaerer);
                session.getTransaction().commit();
                update();
            } catch (NonUniqueObjectException e) {
                currentLaerer.getLaererevaluerings().remove(lev);
                JOptionPane.showMessageDialog(null, "Der findes allerede en anden evaluering med denne text", "Info", 2);
            }
        }
    }//GEN-LAST:event_btnGemEvalueringBeskrivelseActionPerformed

    private void btnSletEvalueringBeskrivelseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletEvalueringBeskrivelseActionPerformed
        if (cbGemteEvalueringer.getItemCount() > 1) {
            session.beginTransaction();
            currentLaerer.getLaererevaluerings().remove((Laererevaluering) cbGemteEvalueringer.getSelectedItem());
            session.delete(cbGemteEvalueringer.getSelectedItem());
            cbGemteEvalueringer.remove(cbGemteEvalueringer.getSelectedIndex());
            session.getTransaction().commit();
        }
    }//GEN-LAST:event_btnSletEvalueringBeskrivelseActionPerformed

    private void btnGemEvalueringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemEvalueringActionPerformed
        if (listEvalueringer.getSelectedValue() != null) {
            session.beginTransaction();
            Evaluering ev = (Evaluering) listEvalueringer.getSelectedValue();
            ev.setBeskrivelse(txtaEvaluering.getText());
            session.update(ev);
            session.getTransaction().commit();
            update();
        }
    }//GEN-LAST:event_btnGemEvalueringActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGemEvaluering;
    private javax.swing.JButton btnGemEvalueringBeskrivelse;
    private javax.swing.JButton btnGemHandleplan;
    private javax.swing.JButton btnGemNyEvaluering;
    private javax.swing.JButton btnGemNyHandleplan;
    private javax.swing.JButton btnInsertEvalueringBeskrivelse;
    private javax.swing.JButton btnSletEvaluering;
    private javax.swing.JButton btnSletEvalueringBeskrivelse;
    private javax.swing.JButton btnSletHandlePlan;
    private javax.swing.JComboBox cbElev;
    private javax.swing.JComboBox cbFag;
    private javax.swing.JComboBox cbGemteEvalueringer;
    private javax.swing.JComboBox cblaerer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList listEvalueringer;
    private javax.swing.JList listHandleplaner;
    private javax.swing.JTextField txtNyEvaluering;
    private javax.swing.JTextArea txtaEvaluering;
    private javax.swing.JTextArea txtaHandleplan;
    // End of variables declaration//GEN-END:variables
}
